package org.glorylight.hacks.social.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glorylight.hacks.social.data.Asset;
import org.glorylight.hacks.social.data.ScrapBookData;
import org.glorylight.hacks.social.data.TimeLine;
import org.glorylight.hacks.social.data.TimeLineDetail;
import org.glorylight.hacks.social.data.TimeLineWrapper;
import org.glorylight.hacks.social.service.ScrapBookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@Path("/timeline")
public class TimelineController extends AbstractBaseController {

	private static final Logger log = LoggerFactory.getLogger(TimelineController.class);
	
	@Autowired
	private ScrapBookService scrapBookService;
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public TimeLineWrapper getTimeLine() {
		TimeLine timeLine = new TimeLine();
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy,MM,dd");
		timeLine.setHeadline("test");
		timeLine.setType("default");
		timeLine.setText("hello world");
		Asset asset = new Asset();
		//TODO : How to define the cover picture??
		asset.setMedia("resources/preparationmage/jocelynn1.png");
		timeLine.setAsset(asset);
		List<ScrapBookData> scrapBookDatas = scrapBookService.findAllByUser(getActiveLogin());
		List<TimeLineDetail> jsonDateField = new ArrayList<TimeLineDetail>();
		for(ScrapBookData scrapBookData : scrapBookDatas){
			TimeLineDetail timeLineDetail = new TimeLineDetail();
			timeLineDetail.setStartDate(dateFormatter.format(scrapBookData.getDateCreated()));
			timeLineDetail.setEndDate(dateFormatter.format(scrapBookData.getDateCreated()));
			timeLineDetail.setHeadline(scrapBookData.getTitle());
			timeLineDetail.setText(scrapBookData.getDesc());
			Asset detailAsset = new Asset();
			detailAsset.setMedia(scrapBookData.getPicPath());
			timeLineDetail.setAsset(detailAsset);
			jsonDateField.add(timeLineDetail);
		}
		timeLine.setDate(jsonDateField);
		
		List<TimeLineDetail> eraField = new ArrayList<TimeLineDetail>();
		TimeLineDetail eraDetail = new TimeLineDetail();
		eraDetail.setStartDate(dateFormatter.format(new Date()));
		eraDetail.setEndDate(dateFormatter.format(new Date()));
		timeLine.setEra(eraField);
		
		List<TimeLineDetail> chartField = new ArrayList<TimeLineDetail>();
		TimeLineDetail chartDetail = new TimeLineDetail();
		chartDetail.setStartDate(dateFormatter.format(new Date()));
		chartDetail.setEndDate(dateFormatter.format(new Date()));
		timeLine.setChart(chartField);
		return new TimeLineWrapper(timeLine);
	}
}
